//
//  AutocompleteTableView.m
//  jungoo
//
//  Created by Sinbad Fly on 8/20/12.
//  Copyright (c) 2012 ABOM. All rights reserved.
//

#import "AutocompleteView.h"

@implementation AutocompleteView

@synthesize tableView = _tableView, dataSource = _dataSource, keyName = _keyName, delegate = _delegate, userInfo = _userInfo;

#pragma mark - Init

- (id)init
{
    if ((self = [super init])) {
        _tableView = [[[UITableView alloc] initWithFrame:self.frame] autorelease];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundView = nil;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.autoresizingMask =   UIViewAutoresizingFlexibleLeftMargin |
                                        UIViewAutoresizingFlexibleWidth |
                                        UIViewAutoresizingFlexibleRightMargin |
                                        UIViewAutoresizingFlexibleTopMargin |
                                        UIViewAutoresizingFlexibleHeight|
                                        UIViewAutoresizingFlexibleBottomMargin;
        
        [self addSubview:_tableView];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        _tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)] autorelease];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundView = nil;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 225, 0);
        _tableView.autoresizingMask =   UIViewAutoresizingFlexibleLeftMargin |
        UIViewAutoresizingFlexibleWidth |
        UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleTopMargin |
        UIViewAutoresizingFlexibleHeight|
        UIViewAutoresizingFlexibleBottomMargin;        
        
        [self setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_tableView];
    }
    
    return self;
    
}

#pragma mark - Public Method

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring 
{    
    [autocompleteDataSource release]; 
    
    autocompleteDataSource = [NSMutableArray new];
    
    for (NSDictionary *aUniversity in _dataSource) {
        
        NSString *curString = [aUniversity objectForKey:_keyName];        
        NSRange substringRange = [curString rangeOfString:substring options:NSCaseInsensitiveSearch];
        
        if (substringRange.length != 0) {
            [autocompleteDataSource addObject:curString];  
        }
    }
    
    [_tableView reloadData];
}

#pragma mark - TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [autocompleteDataSource count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";    
    UITableViewCell *cell = nil;
    NSString *aString = [autocompleteDataSource objectAtIndex:indexPath.row];
    
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }
    
    cell.textLabel.text = aString;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *aString = [autocompleteDataSource objectAtIndex:indexPath.row];        
    
    if ([_delegate respondsToSelector:@selector(autocompleteView:didSeletedWithString:)]) {
        [_delegate autocompleteView:self didSeletedWithString:aString];
    }
}

@end
